package com.fortune.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fortune.dto.NewsDTO;
import com.fortune.entity.NewsEntity;
import com.fortune.mapper.NewsMapper;
import com.fortune.service.NewsService;
import com.fortune.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: fortune
 * @description:
 * @author: wwb
 * @create: 2025-03-25 17:25
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public PageResult<List<NewsEntity>> getNewsList(NewsDTO newsDTO) {

        int pageNum = newsDTO.getPageNum() != null ? newsDTO.getPageNum() : 1;
        int pageSize = newsDTO.getPageSize() != null ? newsDTO.getPageSize() : 10;
        PageHelper.startPage(pageNum,pageSize, true);

        QueryWrapper<NewsEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id","news_title","cover_url","show_more","del_flg","create_time","update_time");
        if(StringUtils.isNotBlank(newsDTO.getCreateTime())){
            wrapper.lt("create_time",newsDTO.getCreateTime());
            pageSize = 100;
        }
        String typeCodeStr = newsDTO.getTypeCode();
        if(StringUtils.isNotBlank(typeCodeStr) &&
            !("0".equals(typeCodeStr) || "undefined".equals(typeCodeStr) || "recommend".equals(typeCodeStr))){
            wrapper.eq("type_code",newsDTO.getTypeCode());
        }
        wrapper.eq("del_flg","0");
        wrapper.eq("news_status","1");
        wrapper.orderByDesc("create_time");
        List<NewsEntity> list = newsMapper.selectList(wrapper);
        PageInfo<NewsEntity> pageInfo = new PageInfo<>(list);
        PageHelper.clearPage();
        return new PageResult<>(Integer.parseInt(pageInfo.getTotal() + ""), pageInfo.getList(),pageNum,pageSize);
    }

    @Override
    public NewsEntity getNewsDetailsById(Integer newsId) {
        return newsMapper.selectById(newsId);
    }


    //提·

}
